package jsqlite;

import com.sangfor.ssl.service.utils.IGeneral;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;

/* loaded from: classes2.dex */
public abstract class Benchmark {
    public static final int ACCOUNT = 2;
    public static final int BRANCH = 1;
    public static final int TELLER = 0;
    static int n_clients = 10;
    static int n_txn_per_client = 10;
    public static int naccounts = 50000;
    public static int nbranches = 1;
    public static int nhistory = 864000;
    public static int ntellers = 10;
    static boolean prepared_stmt = false;
    public static int tps = 1;
    static boolean transactions = true;
    static boolean verbose = false;
    int failed_transactions = 0;
    int transaction_count = 0;
    long start_time = 0;

    public abstract Connection connect(String str, String str2, String str3);

    public void run(String[] strArr) {
        int i;
        int i2 = 0;
        String str = "";
        String str2 = str;
        String str3 = str2;
        String str4 = str3;
        boolean z = false;
        while (i2 < strArr.length) {
            if (strArr[i2].equals("-clients")) {
                i = i2 + 1;
                if (i < strArr.length) {
                    n_clients = Integer.parseInt(strArr[i]);
                    i2 = i;
                    i2++;
                } else {
                    i2++;
                }
            } else if (strArr[i2].equals("-driver")) {
                i = i2 + 1;
                if (i < strArr.length) {
                    str = strArr[i];
                    i2 = i;
                    i2++;
                } else {
                    i2++;
                }
            } else if (strArr[i2].equals("-url")) {
                i = i2 + 1;
                if (i < strArr.length) {
                    str2 = strArr[i];
                    i2 = i;
                    i2++;
                } else {
                    i2++;
                }
            } else if (strArr[i2].equals("-user")) {
                i = i2 + 1;
                if (i < strArr.length) {
                    str3 = strArr[i];
                    i2 = i;
                    i2++;
                } else {
                    i2++;
                }
            } else if (strArr[i2].equals("-password")) {
                i = i2 + 1;
                if (i < strArr.length) {
                    str4 = strArr[i];
                    i2 = i;
                    i2++;
                } else {
                    i2++;
                }
            } else if (strArr[i2].equals("-tpc")) {
                i = i2 + 1;
                if (i < strArr.length) {
                    n_txn_per_client = Integer.parseInt(strArr[i]);
                    i2 = i;
                    i2++;
                } else {
                    i2++;
                }
            } else {
                if (strArr[i2].equals("-init")) {
                    z = true;
                } else if (strArr[i2].equals("-tps")) {
                    i = i2 + 1;
                    if (i < strArr.length) {
                        tps = Integer.parseInt(strArr[i]);
                        i2 = i;
                    }
                } else if (strArr[i2].equals("-v")) {
                    verbose = true;
                }
                i2++;
            }
        }
        if (str.length() == 0 || str2.length() == 0) {
            System.out.println("JDBC based benchmark program\n\nJRE usage:\n\njava jsqlite.BenchmarkDriver -url [url_to_db] \\\n    [-user [username]] [-password [password]] [-driver [driver_class_name]] \\\n    [-v] [-init] [-tpc N] [-tps N] [-clients N]\n");
            System.out.println("OJEC usage:\n\ncvm jsqlite.BenchmarkDataSource [-user [username]] [-password [password]] [-driver [driver_class_name]] \\\n    [-v] [-init] [-tpc N] [-tps N] [-clients N]\n");
            System.out.println();
            System.out.println("-v          verbose mode");
            System.out.println("-init       initialize the tables");
            System.out.println("-tpc N      transactions per client");
            System.out.println("-tps N      scale factor");
            System.out.println("-clients N  number of simultaneous clients/threads");
            System.out.println();
            System.out.println("Default driver class is jsqlite.JDBCDriver");
            System.out.println("in this case use an -url parameter of the form");
            System.out.println("  jdbc:sqlite:/[path]");
            System.exit(1);
        }
        PrintStream printStream = System.out;
        printStream.println("Driver: " + str);
        PrintStream printStream2 = System.out;
        printStream2.println("URL:" + str2);
        System.out.println();
        PrintStream printStream3 = System.out;
        printStream3.println("Scale factor value: " + tps);
        PrintStream printStream4 = System.out;
        printStream4.println("Number of clients: " + n_clients);
        PrintStream printStream5 = System.out;
        printStream5.println("Number of transactions per client: " + n_txn_per_client);
        System.out.println();
        try {
            benchmark(str2, str3, str4, z);
        } catch (java.lang.Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public void benchmark(String str, String str2, String str3, boolean z) {
        Vector vector = new Vector();
        if (z) {
            try {
                try {
                    System.out.print("Initializing dataset...");
                    createDatabase(str, str2, str3);
                    System.out.println("done.\n");
                } catch (java.lang.Exception e) {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
            } finally {
                System.exit(0);
            }
        }
        System.out.println("* Starting Benchmark Run *");
        transactions = false;
        prepared_stmt = false;
        this.start_time = System.currentTimeMillis();
        for (int i = 0; i < n_clients; i++) {
            BenchmarkThread benchmarkThread = new BenchmarkThread(n_txn_per_client, str, str2, str3, this);
            benchmarkThread.start();
            vector.addElement(benchmarkThread);
        }
        Enumeration elements = vector.elements();
        while (elements.hasMoreElements()) {
            ((Thread) elements.nextElement()).join();
        }
        vector.removeAllElements();
        reportDone();
        transactions = true;
        prepared_stmt = false;
        this.start_time = System.currentTimeMillis();
        for (int i2 = 0; i2 < n_clients; i2++) {
            BenchmarkThread benchmarkThread2 = new BenchmarkThread(n_txn_per_client, str, str2, str3, this);
            benchmarkThread2.start();
            vector.addElement(benchmarkThread2);
        }
        Enumeration elements2 = vector.elements();
        while (elements2.hasMoreElements()) {
            ((Thread) elements2.nextElement()).join();
        }
        vector.removeAllElements();
        reportDone();
        transactions = false;
        prepared_stmt = true;
        this.start_time = System.currentTimeMillis();
        for (int i3 = 0; i3 < n_clients; i3++) {
            BenchmarkThread benchmarkThread3 = new BenchmarkThread(n_txn_per_client, str, str2, str3, this);
            benchmarkThread3.start();
            vector.addElement(benchmarkThread3);
        }
        Enumeration elements3 = vector.elements();
        while (elements3.hasMoreElements()) {
            ((Thread) elements3.nextElement()).join();
        }
        vector.removeAllElements();
        reportDone();
        transactions = true;
        prepared_stmt = true;
        this.start_time = System.currentTimeMillis();
        for (int i4 = 0; i4 < n_clients; i4++) {
            BenchmarkThread benchmarkThread4 = new BenchmarkThread(n_txn_per_client, str, str2, str3, this);
            benchmarkThread4.start();
            vector.addElement(benchmarkThread4);
        }
        Enumeration elements4 = vector.elements();
        while (elements4.hasMoreElements()) {
            ((Thread) elements4.nextElement()).join();
        }
        vector.removeAllElements();
        reportDone();
    }

    public void reportDone() {
        double currentTimeMillis = System.currentTimeMillis();
        double d = this.start_time;
        Double.isNaN(currentTimeMillis);
        Double.isNaN(d);
        double d2 = (currentTimeMillis - d) / 1000.0d;
        System.out.println("\n* Benchmark Report *");
        System.out.print("* Featuring ");
        if (prepared_stmt) {
            System.out.print("<prepared statements> ");
        } else {
            System.out.print("<direct queries> ");
        }
        if (transactions) {
            System.out.print("<transactions> ");
        } else {
            System.out.print("<auto-commit> ");
        }
        System.out.println("\n--------------------");
        System.out.println("Time to execute " + this.transaction_count + " transactions: " + d2 + " seconds.");
        System.out.println(String.valueOf(this.failed_transactions) + " / " + this.transaction_count + " failed to complete.");
        double d3 = (double) (this.transaction_count - this.failed_transactions);
        Double.isNaN(d3);
        double d4 = d3 / d2;
        System.out.println("Transaction rate: " + d4 + " txn/sec.");
        this.transaction_count = 0;
        this.failed_transactions = 0;
        System.gc();
    }

    public synchronized void incrementTransactionCount() {
        this.transaction_count++;
    }

    public synchronized void incrementFailedTransactionCount() {
        this.failed_transactions++;
    }

    void createDatabase(String str, String str2, String str3) throws java.lang.Exception {
        PreparedStatement preparedStatement;
        Connection connect = connect(str, str2, str3);
        String databaseProductName = connect.getMetaData().getDatabaseProductName();
        PrintStream printStream = System.out;
        printStream.println("DBMS: " + databaseProductName);
        transactions = true;
        try {
            connect.setAutoCommit(false);
            System.out.println("In transaction mode");
        } catch (SQLException unused) {
            transactions = false;
        }
        try {
            Statement createStatement = connect.createStatement();
            ResultSet executeQuery = createStatement.executeQuery("SELECT count(*) FROM accounts");
            createStatement.clearWarnings();
            int i = 0;
            while (executeQuery.next()) {
                i = executeQuery.getInt(1);
            }
            if (transactions) {
                connect.commit();
            }
            createStatement.close();
            if (i == naccounts * tps) {
                System.out.println("Already initialized");
                connectClose(connect);
                return;
            }
        } catch (java.lang.Exception unused2) {
        }
        System.out.println("Drop old tables if they exist");
        try {
            Statement createStatement2 = connect.createStatement();
            createStatement2.execute("DROP TABLE history");
            createStatement2.clearWarnings();
            createStatement2.execute("DROP TABLE accounts");
            createStatement2.clearWarnings();
            createStatement2.execute("DROP TABLE tellers");
            createStatement2.clearWarnings();
            createStatement2.execute("DROP TABLE branches");
            createStatement2.clearWarnings();
            if (transactions) {
                connect.commit();
            }
            createStatement2.close();
        } catch (java.lang.Exception unused3) {
        }
        System.out.println("Creates tables");
        try {
            Statement createStatement3 = connect.createStatement();
            StringBuilder sb = new StringBuilder(String.valueOf("CREATE TABLE branches (Bid INTEGER NOT NULL PRIMARY KEY,"));
            sb.append("Bbalance INTEGER,");
            createStatement3.execute(String.valueOf(sb.toString()) + "filler CHAR(88))");
            createStatement3.clearWarnings();
            StringBuilder sb2 = new StringBuilder(String.valueOf("CREATE TABLE tellers (Tid INTEGER NOT NULL PRIMARY KEY,"));
            sb2.append("Bid INTEGER,");
            StringBuilder sb3 = new StringBuilder(String.valueOf(String.valueOf(sb2.toString()) + "Tbalance INTEGER,"));
            sb3.append("filler CHAR(84))");
            createStatement3.execute(sb3.toString());
            createStatement3.clearWarnings();
            StringBuilder sb4 = new StringBuilder(String.valueOf("CREATE TABLE accounts (Aid INTEGER NOT NULL PRIMARY KEY,"));
            sb4.append("Bid INTEGER,");
            StringBuilder sb5 = new StringBuilder(String.valueOf(String.valueOf(sb4.toString()) + "Abalance INTEGER,"));
            sb5.append("filler CHAR(84))");
            createStatement3.execute(sb5.toString());
            createStatement3.clearWarnings();
            StringBuilder sb6 = new StringBuilder(String.valueOf(String.valueOf("CREATE TABLE history (Tid INTEGER,") + "Bid INTEGER,"));
            sb6.append("Aid INTEGER,");
            StringBuilder sb7 = new StringBuilder(String.valueOf(String.valueOf(sb6.toString()) + "delta INTEGER,"));
            sb7.append("tstime TIMESTAMP,");
            createStatement3.execute(String.valueOf(sb7.toString()) + "filler CHAR(22))");
            createStatement3.clearWarnings();
            if (transactions) {
                connect.commit();
            }
            createStatement3.close();
        } catch (java.lang.Exception unused4) {
        }
        System.out.println("Delete elements in table in case DROP didn't work");
        try {
            Statement createStatement4 = connect.createStatement();
            createStatement4.execute("DELETE FROM history");
            createStatement4.clearWarnings();
            createStatement4.execute("DELETE FROM accounts");
            createStatement4.clearWarnings();
            createStatement4.execute("DELETE FROM tellers");
            createStatement4.clearWarnings();
            createStatement4.execute("DELETE FROM branches");
            createStatement4.clearWarnings();
            if (transactions) {
                connect.commit();
            }
            prepared_stmt = true;
            try {
                preparedStatement = connect.prepareStatement("INSERT INTO branches(Bid,Bbalance) VALUES (?,0)");
                System.out.println("Using prepared statements");
            } catch (SQLException unused5) {
                prepared_stmt = false;
                preparedStatement = null;
            }
            System.out.println("Insert data in branches table");
            for (int i2 = 0; i2 < nbranches * tps; i2++) {
                if (prepared_stmt) {
                    preparedStatement.setInt(1, i2);
                    preparedStatement.executeUpdate();
                    preparedStatement.clearWarnings();
                } else {
                    createStatement4.executeUpdate("INSERT INTO branches(Bid,Bbalance) VALUES (" + i2 + ",0)");
                }
                if (i2 % 100 == 0 && transactions) {
                    connect.commit();
                }
            }
            if (prepared_stmt) {
                preparedStatement.close();
            }
            if (transactions) {
                connect.commit();
            }
            if (prepared_stmt) {
                preparedStatement = connect.prepareStatement("INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (?,?,0)");
            }
            System.out.println("Insert data in tellers table");
            for (int i3 = 0; i3 < ntellers * tps; i3++) {
                if (prepared_stmt) {
                    preparedStatement.setInt(1, i3);
                    preparedStatement.setInt(2, i3 / ntellers);
                    preparedStatement.executeUpdate();
                    preparedStatement.clearWarnings();
                } else {
                    createStatement4.executeUpdate("INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (" + i3 + "," + (i3 / ntellers) + ",0)");
                }
                if (i3 % 100 == 0 && transactions) {
                    connect.commit();
                }
            }
            if (prepared_stmt) {
                preparedStatement.close();
            }
            if (transactions) {
                connect.commit();
            }
            if (prepared_stmt) {
                preparedStatement = connect.prepareStatement("INSERT INTO accounts(Aid,Bid,Abalance) VALUES (?,?,0)");
            }
            System.out.println("Insert data in accounts table");
            for (int i4 = 0; i4 < naccounts * tps; i4++) {
                if (prepared_stmt) {
                    preparedStatement.setInt(1, i4);
                    preparedStatement.setInt(2, i4 / naccounts);
                    preparedStatement.executeUpdate();
                    preparedStatement.clearWarnings();
                } else {
                    createStatement4.executeUpdate("INSERT INTO accounts(Aid,Bid,Abalance) VALUES (" + i4 + "," + (i4 / naccounts) + ",0)");
                }
                if (i4 % IGeneral.LINE_CONN_SERVICE_TIMEOUT == 0 && transactions) {
                    connect.commit();
                }
                if (i4 > 0 && i4 % IGeneral.LINE_CONN_SERVICE_TIMEOUT == 0) {
                    PrintStream printStream2 = System.out;
                    printStream2.println("\t" + i4 + "\t records inserted");
                }
            }
            if (prepared_stmt) {
                preparedStatement.close();
            }
            if (transactions) {
                connect.commit();
            }
            PrintStream printStream3 = System.out;
            printStream3.println("\t" + (naccounts * tps) + "\t records inserted");
            createStatement4.close();
        } catch (java.lang.Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        connectClose(connect);
    }

    public static int getRandomInt(int i, int i2) {
        double random = Math.random();
        double d = (i2 - i) + 1;
        Double.isNaN(d);
        return ((int) (random * d)) + i;
    }

    public static int getRandomID(int i) {
        int i2;
        int i3;
        int i4 = naccounts;
        int i5 = 0;
        if (i == 0) {
            i5 = 0 + nbranches;
            i2 = ntellers;
        } else if (i != 1) {
            if (i != 2) {
                i3 = 0;
                return getRandomInt(i5, i3);
            }
            i3 = (i4 + i5) - 1;
            return getRandomInt(i5, i3);
        } else {
            i2 = i4;
        }
        if (i == 1) {
            i2 = nbranches;
        }
        i5 += i4;
        i4 = i2;
        i3 = (i4 + i5) - 1;
        return getRandomInt(i5, i3);
    }

    public static void connectClose(Connection connection) {
        if (connection == null) {
            return;
        }
        try {
            connection.close();
        } catch (java.lang.Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
